package com.njust.RateMyTray_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njust.RateMyTray_backend.common.ResourceNotFoundException;
import com.njust.RateMyTray_backend.config.JwtAuthInterceptor;
import com.njust.RateMyTray_backend.dto.ReportCreateDTO;
import com.njust.RateMyTray_backend.dto.ReportProcessDTO;
import com.njust.RateMyTray_backend.entity.Report;
import com.njust.RateMyTray_backend.mapper.ReportMapper;
import com.njust.RateMyTray_backend.service.*;
import com.njust.RateMyTray_backend.vo.ReportListVO;
import com.njust.RateMyTray_backend.vo.ReportVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Autowired
    private CommentService commentService;

    @Lazy
    @Autowired
    private DishService dishService;

    @Autowired
    private WindowService windowService;

    @Autowired
    private ValidationService validationService;

    @Override
    public void createReport(ReportCreateDTO reportCreateDTO) {
        validationService.validateTargetExists(reportCreateDTO.getReportedContentType(), reportCreateDTO.getReportedContentId());
        Report report = new Report();
        
        // 手动设置字段，避免字段名不匹配的问题
        report.setReporterId(reportCreateDTO.getReporterId());
        report.setReportedContentType(reportCreateDTO.getReportedContentType());
        report.setReportedContentId(reportCreateDTO.getReportedContentId());
        report.setReason(reportCreateDTO.getReportReason()); // DTO的reportReason映射到Entity的reason
        report.setStatus("pending");
        report.setCreatedAt(LocalDateTime.now());
        
        this.save(report);
    }

    @Override
    public ReportListVO getReports(String status, String reportedContentType) {
        List<Report> reports = baseMapper.selectReports(status, reportedContentType);
        List<ReportVO> reportVOs = reports.stream()
                .map(this::convertToReportVO)
                .collect(Collectors.toList());
        return new ReportListVO(reportVOs.size(), reportVOs);
    }

    @Override
    @Transactional
    public void processReport(Long reportId, ReportProcessDTO processDTO) {
        Report report = this.getById(reportId);
        if (report == null) {
            throw new ResourceNotFoundException("举报不存在");
        }

        // 更新举报状态
        report.setStatus(processDTO.getStatus());
        report.setProcessorId(Long.valueOf(JwtAuthInterceptor.getCurrentUserClaims().get("userId").toString()));
        report.setUpdatedAt(LocalDateTime.now());
        this.updateById(report);

        // 如果有进一步操作，执行它
        if (processDTO.getActionTaken() != null) {
            String actionType = processDTO.getActionTaken().getType();
            Long contentId = processDTO.getActionTaken().getContentId();
            switch (actionType) {
                case "takedownComment":
                    commentService.removeById(contentId);
                    break;
                case "takedownDish":
                    dishService.removeById(contentId);
                    break;
                case "takedownWindow":
                    windowService.removeById(contentId);
                    break;
                default:
                    // 可选：如果是不支持的操作类型，可以抛出异常
                    break;
            }
        }
    }

    private ReportVO convertToReportVO(Report report) {
        ReportVO reportVO = new ReportVO();
        reportVO.setReportId(report.getId());
        reportVO.setReporterId(report.getReporterId());
        reportVO.setReportedContentType(report.getReportedContentType());
        reportVO.setReportedContentId(report.getReportedContentId());
        reportVO.setReportReason(report.getReason());
        reportVO.setReportTime(report.getCreatedAt());
        reportVO.setStatus(report.getStatus());
        reportVO.setProcessorId(report.getProcessorId());
        reportVO.setProcessingTime(report.getUpdatedAt());
        reportVO.setProcessingResult(null); // 暂时设为null，等待后续数据库字段添加
        return reportVO;
    }
} 